<?php
    /**
     * *获取主键前缀的方法
     * @param  [type] $model [description]
     * @return [type]        [description]
     */
    function columnKeyPrefix($model){
        $model=is_string($model)?M($model):$model;
        $pk=$model->getPk();
        $pk=explode('_',$pk);
        return $pk[0];
    }
    /**
     * [columnKey 获取主键]
     * @param  [type] $model [description]
     * @return [type]        [description]
     */
    function columnKey($model){
        $model=is_string($model)?M($model):$model;
        $pk=$model->getPk();
        return $pk;
    }
    /**
     * *根据数据表全名返回驼峰写法的model名称
     * @param  [type] $tablename [description]
     * @return [type]            [description]
     */
    function toModeName($tablename){
        $array=explode('_',$tablename);
        array_shift($array);
        foreach ($array as  &$value) {
            $value=ucfirst($value);  
        }
        $mname=implode('',$array);
        return $mname;
    }
    /**
     * [getList 查询集口]
     * @param  [type]  $model   [description]
     * @param  [type]  $field   [description]
     * @param  [type]  $map     [description]
     * @param  string  $orderby [description]
     * @param  boolean $order   [description]
     * @param  boolean $display [description]
     * @param  integer $rows    [description]
     * @return [type]           [description]
     */
    function getList($model,$field,$map,$orderby='',$order=false,$display=true,$rows=10){
         $model=checkModel($model);
         //默认按标识符降序排序
         $orderby=$orderby?$model->getPk():$orderby;
         $order=$order?'asc':'desc';
         $count=$model->where($map)->count();
         $Page = new \Think\Page($count,$rows);
         $Page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
         $show= $Page->show();
         $data=$model
              ->field($field)
              ->where($map)
              ->order($orderby,$order)
              ->limit($Page->firstRow.','.$Page->listRows)
              ->select();

         if($display){
            $this->assign('list',$data);
            $this->assign('page',$show);
         }else{
            return array('mainData'=>$data,'page'=>$show,'total'=>$count);
         }

    }
    function simList($model,$map,$field,$select='select',$orderby="",$asc=false,$limit){
         $asc=$asc?'asc':'desc';
         $order=$orderby?$orderby.'  '.$asc:'';

         $data=$model
              ->where($map)
              ->field($field)
              ->order($order)
              ->limit($limit)
              ->$select();
         return $data;
    }
    /**
     * *
     * @param  [type] $model [获得模型名称]
     * @return [type]        [description]
     */
     function checkModel($model){
        if(empty($model)){
            if(class_exists(MODULE_NAME.'\Model\\'.CONTROLLER_NAME . 'ViewModel')){
                return D(CONTROLLER_NAME.'View');
            }
            return D(CONTROLLER_NAME); 
            
        }else{
            if (is_string($model))
            return D($model); 
            else return $model;
        }
        
        
    }
    /**
     * **
     * @return [type] [删除行为集口]
     */
    function DoDelete($map,$model=''){
        $model=$model?$model:checkmodel();
        $model->where($map)->delete();
    }
    /**
     * [ArrayToOne 从多位数组中提取一维数组]
     * @param [type] $array [目标数组]
     * @param [type] $id    [description]
     */
    function ArrayToOne($array,$id){
       $return=array();
       foreach ($array as $value) {
          if($value[$id])
          array_push($return, $value[$id]);
       }
       return $return;
    }
     
    function form($title,$name,$type,$value,$array){
        R('Admin/Form/all',array(
            'title'=>$title,
            'name'=>$name,
            'type'=>$type,
            'value'=>$value,
            'array'=>$array
        ));

    }
    
    function username($uid){
        $username=M('User')->where(array('uid'=>$uid))->getField('user_name');
        return $username;
    }
    function getaddress($ip){
      $Ip = new \Org\Net\IpLocation('UTFWry.dat'); // 实例化类 参数表示IP地址库文件
      $area = $Ip->getlocation($ip); // 获取某个IP地址所在的位置
      if (empty($area)){
         $area='未知位置';
      }
      return $area;
    }
    function mapStatus($prefix){
       $map[$prefix.'_status']=1;
       return $map;
    }
    function getUid(){
        return session('uid');
    }
    //成功回调
    function suc($data,$msg="成功"){
        $array=array(
             'status'=>'success',
             'msg'=>$msg,
             'data'=>$data
        ); 
        echo json_encode($array);  
        die(); 
    }
    //成功回调
    function err($msg="失败"){
        $array=array(
             'status'=>'error',
             'msg'=>$msg,
             'data'=>null
        ); 
        echo json_encode($array); 
        die();  
    }
    //返回相册图片
    function album($img){
        $array=explode(',',$img);
        $re=array();
        foreach ($array as $value) {
            if($value){
                array_push($re,'<img width="50px" height="50px" src="'.__ROOT__.$value.'"/>');
            }
        }
        $str=implode(',',$re);
        return $str;
    }
    //返回时间
    function ymdhis($time){
         return date('Y-m-d H:i:s',$time);
    }
    //开启状态
    function status($status){
          if($status==1) return '<span class="btn btn-success">开启状态</span>';
          elseif($status==0) return '<span class="btn btn-danger">关闭状态</span>';
    }
    function getAddressName($id){
          $pro=M('Address')
               ->where(array('addr_id'=>$id))
               //->field(array('addr_id','addr_name'))
               ->getField('addr_name');
          return $pro;
    }
    function img($path){
         return '<img width="50px" height="50px" src="'.__ROOT__.$path.'" />';
    }
    function shiyan(){
        username(1);
    }
?>